﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using TalentPools.Model;

namespace TalentPools.DAL
{
    public class Admin_LoginService
    {
        #region 登录
        public string LoginIn(string username)
        {
            string sql = "select LoginPwd from Admin_Login where LoginName='" + username + "'";
            return DBHelper.ExecuteScalar(CommandType.Text, sql, null).ToString();
        }
        #endregion

        #region 判断是否有权限。默认为无权限
        /// <summary>
        /// 判断是否有权限。默认为无权限
        /// </summary>
        /// <param name="name">Session</param>
        /// <param name="s_purview">如果有这些参数值，则有权限，否则没有。</param>
        /// <returns>默认为无权限</returns>
        public bool Exists_Purview(string name, string s_purview)
        {
            string sql = "select top 1 Purview from Admin_Login where LoginName='" + name + "'";
            using (SqlDataReader dr = DBHelper.ExecuteGetReader(CommandType.Text, sql, null))
            {
                if (dr.Read())
                {
                    if (dr[0].ToString().Contains(s_purview))
                    {
                        return true;
                    }
                }
                return false;
            }
        }
        #endregion

        #region 获取权限
        /// <summary>
        /// 获取权限
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public string GetPurview(string name)
        {
            string purStr = "";
            string sql = "select top 1 Purview from Admin_Login where LoginName='" + name + "'";
            using (SqlDataReader dr = DBHelper.ExecuteGetReader(CommandType.Text, sql, null))
            {
                if (dr.Read())
                {
                    purStr = dr[0].ToString();
                }
                return purStr;
            }
        }
        #endregion

        #region 获取管理员信息
        /// <summary>
        /// 获取管理员信息
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Admin_Login GetAdmin(string name)
        {
            Admin_Login admin = new Admin_Login();
            string sql = "select * from Admin_Login where LoginName='" + name + "'";
            using (SqlDataReader dr = DBHelper.ExecuteGetReader(CommandType.Text, sql, null))
            {
                if (dr.Read())
                {
                    admin.Id = Convert.ToInt32(dr["Id"]);
                    admin.LoginName = dr["LoginName"].ToString();
                    admin.LoginPwd = dr["LoginPwd"].ToString();
                    admin.RealName = dr["RealName"].ToString();
                    admin.Email = dr["Email"].ToString();
                    admin.Purview = dr["Purview"].ToString();
                }
                return admin;
            }
        }
        #endregion

        #region 获取管理员信息
        /// <summary>
        /// 获取管理员信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Admin_Login GetAdmin(int id)
        {
            Admin_Login admin = new Admin_Login();
            string sql = "select * from Admin_Login where Id='" + id + "'";
            using (SqlDataReader dr = DBHelper.ExecuteGetReader(CommandType.Text, sql, null))
            {
                if (dr.Read())
                {
                    admin.Id = Convert.ToInt32(dr["Id"]);
                    admin.LoginName = dr["LoginName"].ToString();
                    admin.LoginPwd = dr["LoginPwd"].ToString();
                    admin.RealName = dr["RealName"].ToString();
                    admin.Email = dr["Email"].ToString();
                    admin.Purview = dr["Purview"].ToString();
                }
                return admin;
            }
        }
        #endregion

        #region 获取管理员信息
        /// <summary>
        /// 获取管理员信息
        /// </summary>
        /// <returns></returns>
        public List<Admin_Login> GetAdmin()
        {
            List<Admin_Login> list = new List<Admin_Login>();
            string sql = "select * from Admin_Login";
            using (SqlDataReader dr = DBHelper.ExecuteGetReader(CommandType.Text, sql, null))
            {
                while (dr.Read())
                {
                    Admin_Login admin = new Admin_Login();
                    admin.Id = Convert.ToInt32(dr["Id"]);
                    admin.LoginName = dr["LoginName"].ToString();
                    admin.LoginPwd = dr["LoginPwd"].ToString();
                    admin.RealName = dr["RealName"].ToString();
                    admin.Email = dr["Email"].ToString();
                    admin.Purview = dr["Purview"].ToString();
                    list.Add(admin);
                }
                return list;
            }
        }
        #endregion

        #region 删除管理员信息
        /// <summary>
        /// 删除管理员信息
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool DeleteAdmin(int id)
        {
            Admin_Login adl = GetAdmin(id);
            if (adl.Purview.Contains("10000"))
            {
                return false;
            }
            string sql = "delete from Admin_Login where Id=@id";
            SqlParameter para = new SqlParameter("@id", id);
            if (DBHelper.ExecuteNonQuery(CommandType.Text, sql, para) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region 查询管理员个数
        /// <summary>
        /// 获取管理员信息
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public int GetAdminCount(string name)
        {
            string sql = "select Count(*) from Admin_Login where LoginName='" + name + "'";
            return (int)DBHelper.ExecuteScalar(CommandType.Text, sql, null);
        }
        #endregion

        #region 插入管理员
        /// <summary>
        /// 插入管理员信息
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool InsertAdmin(Admin_Login admin)
        {
            string sql = "Insert into Admin_Login(LoginName,LoginPwd,RealName,Purview,Email)values(@LoginName,@LoginPwd,@RealName,@Purview,@Email)";
            SqlParameter[] paras = new SqlParameter[] { 
            new SqlParameter("@LoginName",admin.LoginName),
            new SqlParameter("@LoginPwd",admin.LoginPwd),
            new SqlParameter("@RealName",admin.RealName),
            new SqlParameter("@Purview",admin.Purview),
            new SqlParameter("@Email",admin.Email)
            };
            return DBHelper.ExecuteNonQuery(CommandType.Text, sql, paras) > 0;

        }
        #endregion

        #region 修改管理员
        /// <summary>
        ///  修改邮箱、密码
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool UpdateAdmin(Admin_Login admin)
        {
            string sql = "Update Admin_Login set LoginPwd=@LoginPwd,Email=@Email where Id=@Id)";
            SqlParameter[] paras = new SqlParameter[] { 
                new SqlParameter("@Id",admin.Id),
            new SqlParameter("@LoginPwd",admin.LoginPwd),
            new SqlParameter("@Email",admin.Email)
            };
            return DBHelper.ExecuteNonQuery(CommandType.Text, sql, paras) > 0;

        }
        #endregion

        #region 修改管理员
        /// <summary>
        ///  修改邮箱、密码、权限
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool UpdateAdmin1(Admin_Login admin)
        {
            string sql = "Update Admin_Login set LoginPwd=@LoginPwd,Email=@Email,Purview=@Purview where Id=@Id";
            SqlParameter[] paras = new SqlParameter[] { 
                new SqlParameter("@Id",admin.Id),
                 new SqlParameter("@Purview",admin.Purview),
            new SqlParameter("@LoginPwd",admin.LoginPwd),
            new SqlParameter("@Email",admin.Email)
            };
            return DBHelper.ExecuteNonQuery(CommandType.Text, sql, paras) > 0;

        }
        #endregion

        #region 修改管理员
        /// <summary>
        ///  修改邮箱
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool UpdateAdmin2(Admin_Login admin)
        {
            string sql = "Update Admin_Login set Email=@Email where Id=@id";
            SqlParameter[] paras = new SqlParameter[] { 
                new SqlParameter("@Id",admin.Id),
            new SqlParameter("@Email",admin.Email)
            };
            return DBHelper.ExecuteNonQuery(CommandType.Text, sql, paras) > 0;

        }
        #endregion

        #region 修改管理员
        /// <summary>
        ///  修改邮箱，权限
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool UpdateAdmin3(Admin_Login admin)
        {
            string sql = "Update Admin_Login set Email=@Email,Purview=@Purview where Id=@Id";
            SqlParameter[] paras = new SqlParameter[] { 
                new SqlParameter("@Purview",admin.Purview),
                new SqlParameter("@Id",admin.Id),
            new SqlParameter("@Email",admin.Email)
            };
            return DBHelper.ExecuteNonQuery(CommandType.Text, sql, paras) > 0;

        }
        #endregion

        #region 获取密码
        /// <summary>
        ///  修改管理员
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public string GetPwd(string name)
        {
            string sql = "select LoginPwd from Admin_Login where LoginName='" + name + "'";

            using (SqlDataReader dr = DBHelper.ExecuteGetReader(CommandType.Text, sql, null))
            {
                if (dr.Read())
                {
                    return dr[0].ToString();
                }
            }
            return "";

        }
        #endregion
    }
}
